package com.xb.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xb.common.result.Result;
import com.xb.model.system.SysRole;
import com.xb.model.vo.AssginRoleVo;
import com.xb.model.vo.SysRoleQueryVo;
import com.xb.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

//@Api(tags = "角色管理") //Swagger生成api的提示
//@RestController //ResponseBody+Controller ResponseBody:将数据以JSON的格式返回
//@RequestMapping("/admin/system/sysRole")
public class SysRoleController {
    @Resource
    private SysRoleService sysRoleService;

    /**
     * 查询所有
     *
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation(value = "查询所有")//swagger生成api的提示
    @GetMapping("all")
    public Result all() {
        List<SysRole> list = sysRoleService.list();
        return Result.ok(list);
    }

    /**
     *根据id删除
     *
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation(value = "根据id删除")
    @DeleteMapping("remove/{id}")
    public Result remove(@PathVariable("id") Long id) {
        boolean flag = sysRoleService.removeById(id);
        return flag ? Result.ok() : Result.fail();
    }

    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @param roleQueryVo
     * @return
     */
    @ApiOperation(value = "分页查询")
    @GetMapping("/{page}/{limit}")
    public Result page(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,

            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,

            @ApiParam(name = "roleQueryVo", value = "查询对象", required = false)
                    SysRoleQueryVo roleQueryVo) {
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRole> pageModel = sysRoleService.selectPage(pageParam, roleQueryVo);
        return Result.ok(pageModel);
    }

    /**
     * 添加
     * RequestBody 不用于get请求中
     * 将请求体的内容的JSON数据封装到实体类中,可以省略
     *
     * @param sysRole
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation(value = "添加")
    @PostMapping("/save")
    public Result save(@RequestBody SysRole sysRole) {
        boolean flag = sysRoleService.save(sysRole);
        return flag ? Result.ok() : Result.fail();
    }

    /**
     * 根据id获取
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id获取")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        SysRole role = sysRoleService.getById(id);
        return Result.ok(role);
    }

    /**
     * 根据id修改
     *
     * @param role
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation(value = "根据id修改")
    @PutMapping("/update")
    public Result update(@RequestBody SysRole role) {
        boolean flag = sysRoleService.updateById(role);
        return flag ? Result.ok() : Result.fail() ;
    }

    /**
     * 根据id批量删除
     * json的数组格式对应list集合
     * @param idList
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysRole.delete')")
    @ApiOperation(value = "根据id批量删除")
    @DeleteMapping("/batch")
    public Result batch(@RequestBody List<Long> idList) {
        sysRoleService.removeByIds(idList);
        return Result.ok();
    }


    /**
     * 获取用户角色
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户获取角色数据")
    @GetMapping("/roles/{userId}")
    public Result roles(@PathVariable("userId") Long userId) {
        Map<String, Object> roleMap = sysRoleService.getRolesByUserId(userId);
        return Result.ok(roleMap);
    }

    /**
     * 用户分配角色
     * @param assginRoleVo
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.sysUser.assignRole')")
    @ApiOperation(value = "根据用户分配角色")
    @PostMapping("/assign")
    public Result doAssign(@RequestBody AssginRoleVo assginRoleVo) {
        sysRoleService.doAssign(assginRoleVo);
        return Result.ok();
    }
}
